{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 评测几款读取音频的方式的速度差异"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import wave\n",
    "import numpy as np\n",
    "from scipy.io import wavfile\n",
    "import librosa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_wav_data_by_wave(filename):\n",
    "    '''\n",
    "    读取一个wav文件，返回声音信号的时域谱矩阵和播放时间\n",
    "    '''\n",
    "    wav = wave.open(filename,\"rb\") # 打开一个wav格式的声音文件流\n",
    "    num_frame = wav.getnframes() # 获取帧数\n",
    "    num_channel=wav.getnchannels() # 获取声道数\n",
    "    framerate=wav.getframerate() # 获取帧速率\n",
    "    num_sample_width=wav.getsampwidth() # 获取实例的比特宽度，即每一帧的字节数\n",
    "    str_data = wav.readframes(num_frame) # 读取全部的帧\n",
    "    wav.close() # 关闭流\n",
    "    wave_data = np.fromstring(str_data, dtype = np.short) # 将声音文件数据转换为数组矩阵形式\n",
    "    wave_data.shape = -1, num_channel # 按照声道数将数组整形，单声道时候是一列数组，双声道时候是两列的矩阵\n",
    "    wave_data = wave_data.T # 将矩阵转置\n",
    "    #wave_data = wave_data \n",
    "    return wave_data, framerate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_wav_data_by_scipy(filename):\n",
    "    sampling_freq,audio = wavfile.read(filename)\n",
    "    return audio,sampling_freq\n",
    "    \n",
    "def read_wav_data_by_librosa(filename):\n",
    "    data,_ = librosa.core.load(filename,sr=16000)\n",
    "    return data,_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([-0.00213623, -0.00393677, -0.00549316, ..., -0.0007019 ,\n",
       "        -0.00115967, -0.00079346], dtype=float32), 16000)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "read_wav_data_by_librosa(path[0]+\".wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\sailist\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\ipykernel_launcher.py:12: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead\n",
      "  if sys.path[0] == '':\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(array([[ -70, -129, -180, ...,  -23,  -38,  -26]], dtype=int16), 16000)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "read_wav_data_by_wave(path[0]+\".wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ -70, -129, -180, ...,  -23,  -38,  -26], dtype=int16), 16000)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "read_wav_data_by_scipy(path[0]+\".wav\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 准备数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import acoustic.Reader as Reader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "z200 = Reader.Z200()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = z200.load_from_path(\"../Dataset/z200\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['C:\\\\E\\\\jupyter_notebook\\\\voice_reco\\\\Dataset\\\\z200\\\\G0002\\\\session01\\\\T0055G0002S0001',\n",
       " 'C:\\\\E\\\\jupyter_notebook\\\\voice_reco\\\\Dataset\\\\z200\\\\G0002\\\\session01\\\\T0055G0002S0002',\n",
       " 'C:\\\\E\\\\jupyter_notebook\\\\voice_reco\\\\Dataset\\\\z200\\\\G0002\\\\session01\\\\T0055G0002S0004',\n",
       " 'C:\\\\E\\\\jupyter_notebook\\\\voice_reco\\\\Dataset\\\\z200\\\\G0002\\\\session01\\\\T0055G0002S0005']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path[:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试wave"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\sailist\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\ipykernel_launcher.py:12: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead\n",
      "  if sys.path[0] == '':\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "use 0.5578425353205034 seconds.\n"
     ]
    }
   ],
   "source": [
    "start = time.clock()\n",
    "for i in path:\n",
    "    read_wav_data_by_wave(i+\".wav\")\n",
    "end = time.clock()\n",
    "print(f\"use {end-start} seconds.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试scipy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "use 0.45256812690428205 seconds.\n"
     ]
    }
   ],
   "source": [
    "start = time.clock()\n",
    "for i in path:\n",
    "    read_wav_data_by_scipy(i+\".wav\")\n",
    "end = time.clock()\n",
    "print(f\"use {end-start} seconds.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试librosa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "use 0.6088633135516375 seconds.\n"
     ]
    }
   ],
   "source": [
    "start = time.clock()\n",
    "for i in path[:10]:\n",
    "    read_wav_data_by_librosa(i+\".wav\")\n",
    "end = time.clock()\n",
    "print(f\"use {end-start} seconds.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可见由于libsora封装功能过于复杂，读取大量数据较为缓慢，而scipy应该较wave有些许优化，因此读取更加快速"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
